package com.wyl.java.thread_.quit_;

import cn.hutool.core.util.RandomUtil;

/**
 * 停止普通方式启动的线程
 * @author 吴宇亮 on 2023/3/24 16:20
 */
public class QuitThread01 {

    public static void main(String[] args) {
        Thread thread01 = new Thread(() -> {
            while (true) {
                try {
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println(Thread.currentThread().getName() + ": interrupted");
                        break;
                    }
                    Thread.sleep(RandomUtil.randomLong(500, 1200));
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        thread01.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main: interrupted");
        thread01.interrupt();
    }
}
